package com.real8.romemp.client.activity;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.real8.romemp.client.ClientFactory;
import com.real8.romemp.client.GameConstants;
import com.real8.romemp.client.place.GameStartPlace;
import com.real8.romemp.client.place.LabourAllocationPlace;
import com.real8.romemp.client.ui.GameStartView;
import com.real8.romemp.shared.GameData;

public class GameStartActivity extends AbstractActivity {
	private final ClientFactory clientFactory;
	private GameStartView view;

	public GameStartActivity(GameStartPlace place, ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
	}

	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		view = clientFactory.getGameStartView();
		if (view != null) {
			// prepare view
			view.hideAll();

			// prepare game data
			GameData gameData = clientFactory.getGameData();
			gameData.initialize();

			// show game start
			view.showGameStart();

			// TODO consider fancy start

			// delay
			Timer timer = new Timer() {
				@Override
				public void run() {
					// 
					goToNextPlace();
				}
			};
			timer.schedule(GameConstants.DELAY_6000);

			// set widget
			containerWidget.setWidget(view.asWidget());
		}
	}

	private void goToNextPlace() {
		clientFactory.getPlaceController().goTo(new LabourAllocationPlace());
	}
}
